﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AtletIS.Shared.Helpers
{
    public static class RoundingHelper
    {
        // DateTime rounding
        // floor: long ticks = date.Ticks / span.Ticks;
        // round: long ticks = (date.Ticks + (span.Ticks / 2) + 1)/ span.Ticks;
        // ceiling: long ticks = (date.Ticks + span.Ticks - 1)/ span.Ticks;

        public static DateTime RoundUpDateTime(DateTime input, int resultCode)
        {
            TimeSpan span; 
            long ticks;
            
            switch (resultCode)
            {
                // 1 = Čas v stotinách
                case 1:
                    span = new TimeSpan(0, 0, 0, 0, 10); break;

                // 2 = Čas v desatinách
                case 2:
                    span = new TimeSpan(0, 0, 0, 0, 100); break;

                // 3 = Čas v sekundách
                case 3:
                    span = new TimeSpan(0, 0, 0, 1, 0); break;

                default:
                    span = new TimeSpan(0, 0, 0, 0, 1); break;
            }

            ticks = (input.Ticks + span.Ticks - 1) / span.Ticks;    // get ceiling
            return new DateTime(ticks * span.Ticks);
        }
    }
}
